<?php

# get the diagram as select list
function get_diagram_as_select($params = array()) {
    global $diagram_cache;
    # current_id is for diagram edit
    $default_params = array('space' => ' — ', 'selected' => '', 'diagram' => false, 'current_id'=>null);
    $diagram_cache = cache_current_diagram();
    $output = '';
    $params += $default_params;
    if (!empty($params['none'])) {
        $output .= '<option value="0">' . $params['none'] . '</option>' . "\n";
    }
    $output .= get_diagram_select_option_list($params);
    return $output;
}

function get_diagram_select_option_list($params = array(), $parent_id = 0, $level = 0) {
    global $diagram_cache;
    $output = '';
    if (isset($diagram_cache[$parent_id])) {
        $disabled = '';
        $count = count($diagram_cache[$parent_id]);
        for($i = 0; $i < $count; $i++) {
            $is_selected = '';
            $diagram = $diagram_cache[$parent_id][$i];
            if ($diagram->id == $params['current_id']) {
                continue;
            }
            if ($diagram->type == 'item' || $diagram->type == 'page') {
                $disabled = ' disabled="disabled" ';
            } else if ($diagram->type == 'list') {
                $disabled = '';
                # use for post
                if ($diagram->id == $params['selected']) {
                    $is_selected = 'selected';
                }
            }
            if ($params['diagram']) {
                $disabled = '';
                # use for add diagram
                if ($diagram->id == $params['selected']) {
                    $is_selected = 'selected';
                }
            }
            $output .= '<option ' . $disabled . ' value="' . $diagram->id . '" ' . $is_selected . ' >' . str_repeat($params['space'],$level) . attribute_escape($diagram->title) . '</option>' . "\n";
            $output .= get_diagram_select_option_list($params, $diagram->id, $level + 1);
        }
    }
    return $output;
}


function cache_current_diagram() {
    global $aldb;
    $diagram_original = $aldb->get_results("SELECT * FROM $aldb->diagram WHERE status != '0' ORDER BY displayorder ASC");

    $count = count($diagram_original);
    if (empty($count))
        return false;

    for ($i = 0; $i < $count; $i++) {
        if(empty($diagram_original[$i])) {
            break;
        }
        $current_diagram[$diagram_original[$i]->parent][] = $diagram_original[$i];
    }

    return $current_diagram;
}

function get_diagram_as_ul($params = array()) {
    $diagrams = cache_current_diagram();
    $default_params = array('manage'=>false);
    $params += $default_params;
    if (empty($diagrams)) {
        $diagram_list = __('No diagram item, add your diagram.');
    } else {
        $diagram_list = get_diagram_list($diagrams, 0, 0, $params);
    }
    return $diagram_list;
}

function get_diagram_list($diagrams, $parent_id = 0, $i = 0, $params = array()) {
    $list = '';
    if (!empty($diagrams[$parent_id])) {
        if ($parent_id != 0) {
            $list = "\n" . '<ul>' . "\n";
        } else {
            $list = '<ul id="browser" class="filetree">';
        } 
        $class = '';
        foreach($diagrams[$parent_id] as $diagram) {
            $list .= '<li><span class="' . $diagram->type . '" id="diagram_' . $diagram->id . '">' . $diagram->title;
            if ($params['manage']) {
                $list .= ' <span class="add_next_item">' . admin_link(__('Move up'), '/diagram_manage/move_up/' . $diagram->id) . '</span> <span class="edit_item">' . admin_link(__('Move down'), '/diagram_manage/move_down/' . $diagram->id) . '</span> <span class="edit_item">' . admin_link(__('Add child'), '/diagram_manage/diagram_new/' . $diagram->id . '/add_next_item') . '</span> <span class="delete_item">' . admin_link(__('Delete'), '/diagram_manage/diagram_delete/' . $diagram->id, 'onclick="return confirm(\'' . __('Are you sure you want to delete it?') . '\')"') . '</span> <span class="edit_item">' . admin_link(__('Edit'), '/diagram_manage/diagram_new/' . $diagram->id . '/edit/')  . '</span>';
            } else {
                $other_action = '';
                switch($diagram->type) {
                    case 'page':
                        $title = __('Edit');
                        $url = "/diagram/page_edit/$diagram->id/edit/?/diagram";
                        break;
                    case 'list':
                        $title = __('Post New');
                        $url = "/post_new/post_new/$diagram->id/";
                        $other_action = ' <span classs="edit_item">' . admin_link(__('Manage posts'), '/posts/posts/' . $diagram->id) . '</span>';
                        break;
                    case 'item':
                    case 'url':
                    default:
                        $title = '';
                        $url = '';
                }
                $list .= ' <span class="add_next_item">' . admin_link($title, $url) . '</span>' . $other_action;
            } 
            $list .='</span>';
            $list .= get_diagram_list($diagrams, $diagram->id, $i+1, $params);
            $list .= '</li>' . "\n";
        }
        $list .= '</ul>' . "\n";
    }
    return $list;
}

function diagram_delete($diagram_id) {
    global $aldb;
    $childs_list = cache_diagram_childs($diagram_id) . $diagram_id;
    $childs = explode(',', $childs_list);
    foreach($childs as $id) {
        $aldb->query("DELETE FROM $aldb->diagram WHERE id='$id'");
    }
}

function cache_diagram_childs($parent_id) {
    global $aldb;
    $childs_list = '';
    $results = $aldb->get_results("SELECT `id` FROM $aldb->diagram WHERE parent='$parent_id'");
    foreach($results as $result) {
        $childs_list .= $result->id . ',';
        $childs_list .= cache_diagram_childs($result->id);
    }
    return $childs_list;
}

function diagram_moveup($id) {
    return diagram_move($id, 'moveup');
}

function diagram_movedown($id) {
    return diagram_move($id, 'movedown');
}

function diagram_move($id, $action) {
    global $aldb;
    $current_diagram = $aldb->get_row("SELECT parent, displayorder FROM $aldb->diagram WHERE id = '$id' AND status = '1' LIMIT 1");
    $parent_id = $current_diagram->parent;
    $current_order = $current_diagram->displayorder;

    switch($action) {
        case 'moveup':
            $condition = " displayorder < '$current_order' ";
            $orderby = ' ORDER BY displayorder DESC ';
            break;
        case 'movedown':
            $condition = " displayorder > '$current_order' ";
            $orderby = ' ORDER BY displayorder ASC ';
            break;
    }

    $next_diagram = $aldb->get_row("SELECT id, displayorder FROM $aldb->diagram WHERE parent = '$parent_id' AND $condition AND status = '1' $orderby LIMIT 1");
    if (!empty($next_diagram)) {
        $next_id = $next_diagram->id;
        $next_order = $next_diagram->displayorder;
    }
    if (!empty($next_id)) {
        $aldb->query(" UPDATE $aldb->diagram SET displayorder = '$next_order' WHERE id = '$id' ");
        $aldb->query(" UPDATE $aldb->diagram SET displayorder = '$current_order' WHERE id = '$next_id' ");
    } else {
        return false;
    }
    return $id;
}

?>
